package org.teamswift.crow.rbac.security.realms;

import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.credential.HashedCredentialsMatcher;
import org.apache.shiro.crypto.hash.Sha256Hash;
import org.apache.shiro.realm.AuthenticatingRealm;
import org.springframework.stereotype.Component;
import org.teamswift.crow.rbac.common.ICrowUser;
import org.teamswift.crow.rbac.entity.CrowUser;
import org.teamswift.crow.rbac.security.token.CrowVerifyCodeToken;
import org.teamswift.crow.rbac.service.CrowUserService;
import org.teamswift.crow.rest.exception.CrowErrorMessage;
import org.teamswift.crow.rest.exception.impl.DataNotFoundException;
import org.teamswift.crow.rest.utils.CrowBeanUtils;
import org.teamswift.crow.rest.utils.CrowMessageUtil;

import java.util.Optional;

public class CrowMobileRealm extends AuthenticatingRealm {

    @Override
    public boolean supports(AuthenticationToken token) {
        return token instanceof CrowVerifyCodeToken;
    }

    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {

        String mobile = (String) token.getPrincipal();

        CrowUserService crowUserService = CrowBeanUtils.getBean(CrowUserService.class);
        CrowUser user = crowUserService.findOneByMobile(mobile).orElseThrow(() -> {
            throw new DataNotFoundException(
                    CrowMessageUtil.error(CrowErrorMessage.NotFoundByID)
            );
        });


        return null;
    }

}
